/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.service;

import com.factory.server.bo.AbstractBusinessObject;
import com.factory.server.cache.InputCache;
import com.factory.server.helper.InputWrapper;
import com.factory.server.scheduler.SchedulerServiceIface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Saver for inputs. Asynch. stores inputs to DB.
 * @author milos
 */
@Component
public class InputSaver implements Runnable {

//    @Autowired
//    DataServiceIface dataService;
    @Autowired
    private SchedulerServiceIface schedulerService;
    @Autowired
    private DataServiceIface dataService;

    public void run() {
        while (true) {
            InputWrapper inputWrapper = null;
            if (schedulerService.getInputQueue() != null) {
                inputWrapper = schedulerService.getInputQueue().get();
            }
            if (inputWrapper != null) {
                for (AbstractBusinessObject bo : inputWrapper.getDBRecords()) {
                    dataService.store(bo);
                }
                InputCache.remove(inputWrapper.getInputId());
            }
        }
    }
}
